<extend name="Public:base" />
<block name="content">
<div class="row">
	<div class="col-xs-12">
		<form class="form-horizontal"  id="validation-form" enctype="multipart/form-data"  onsubmit="return check()" method="post" action="{:U('Category/addone')}">
			<div class="form-group required">
				<label class="col-sm-2 control-label">分类名称：</label>
				<div class="col-sm-10">
					<input  class="form-control"  style="width:220px;"  type="text" placeholder="" name="category_name"  value="" />
				</div>
			</div>
			<div class="form-group required">
				<label class="col-sm-2 control-label">分类说明：</label>
				<div class="col-sm-10">
					<input  class="form-control" style="width:220px;" type="text" placeholder="" name="desc"  value="" />
				</div>
			</div>
			<div class="form-group required">
				<label class="col-sm-2 control-label">排序：</label>
				<div class="col-sm-10">
					<input  class="form-control" style="width:220px;" type="text" placeholder="" name="order" value=""/>
				</div>
			</div>

			<div class="form-group required">
        <label class="col-sm-2 control-label">状态：</label>
        <label><input type="radio"  name="status" value="1" onclick="" checked="checked"/>开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
        <label><input type="radio" name="status" value="2" onclick="" />关闭</label>
      </div>
      <div class="form-group required">
        <label class="col-sm-2 control-label">上传图片：</label>
        <div class="col-sm-10" id="up">
            <input type="file" name="icon" id="icon" onchange="preview(event)">
        </div>
  	  </div>
			<div class="form-group">
        <label class="col-sm-2 control-label" style="visibility: hidden;">上传图片：</label>
        <div class="col-sm-10">
            <input type="submit" value="提交" class="btn btn-primary radius5 btn-sm">
            <a class="btn btn-primary radius5 btn-sm" href="{:U('Category/one')}"> 返回 </a>
        </div>
      </div>
		</form>
	</div>
</div>
</block>
<block name="javascript">
	<script type="text/javascript">

		function preview(event) {
			var files = event.target.files;
			var reader  = new FileReader();
			reader.onload = function (event) {
				var result = event.target.result;
				var x = document.getElementById("icon");
				if (!x || !x.value) return;
				var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;
				if (patn.test(x.value)) {
					var y = document.getElementById("img");
					if (y) {
						y.src = result;
					} else {
						var img = document.createElement("img");
						img.setAttribute("src", result);
						img.setAttribute("width", "145");
						img.setAttribute("height", "155");
						img.setAttribute("id", "img");
						document.getElementById("up").appendChild(img);
					}
				} else {
					$.jbox.info('您选择的似乎不是图像文件!','提示');
				}
				reader = null;
			}
			reader.readAsDataURL(files[0]);

		}

		function check(){
			var category_name = $('input[name=category_name]').val();
			var desc = $('input[name=desc]').val();
			var order = $('input[name=order]').val();
			var img = document.getElementById("img");
			if($.trim(category_name) ==''){
				$.jbox.info('名称不能为空！','提示');
				return false;
			}
			if($.trim(desc)==''){
				$.jbox.info('说明不能为空！','提示');
				return false;
			}
			if($.trim(order)==''){
				$.jbox.info('排序不能为空！','提示');
				return false;
			}
			if(isNaN(order)){
				$.jbox.info('请输入数字！','提示');
				return false;
			}
			if(!img){
				$.jbox.info('请加入图片！','提示');
				return false;
			}

			$('#validation-form').ajaxSubmit(function(data){
				if(data=='1'){
					$.jbox.tip("添加成功！", 'loading');
					window.setTimeout(function () {window.location.href="{:U('Category/one')}"; }, 500);
				}if(data=='2'){
					$.jbox.info('添加失败！','提示');
				}if(data=='-1'){
					$.jbox.info('图片添加失败！','提示');
				}
			});

			return false;
		}
	</script>
</block>

